package algorithm.sort;

//插入排序
public class InsertSort {

    public static void main(String[] args) {
        int[] data = new int[]{-99,-6,3,78,9,40,-1,1};

        insertSort(data);

        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i]+" ");
        }
    }

    public static void insertSort(int[] data){
        for (int i = 1;i<data.length;i++){
            int value = data[i];
            int insertIndex = i-1;
            //insertIndex>0 防止数组越界
            while (insertIndex>0 && data[insertIndex]>value){
                data[insertIndex+1] = data[insertIndex];
                insertIndex--;
            }
            data[insertIndex+1] = value;
        }


    }
}
